#version 460
#extension GL_GOOGLE_include_directive : enable


#include "../../../../core/shader_tranform.h"
#include "../../../../core/shader_color.h"
#include "../../../../core/shader_widget_panel.h"
#include "../../../../core/shader_widget_color.h"



layout(points) in;
layout(line_strip, max_vertices=8) out;





layout(location = 1) in VS_GS_VERTEX {
	vec2 vPos;
	uint vInstance;
	uint vVertexIndex;
}vertex_in[];


layout(location = 0) out vec4 gColor;
out gl_PerVertex {
	vec4	gl_Position;
};

//vec2 pos[4] = {vec2(10.0, 10.0), vec2(100.0, 10.0), vec2(10.0, 100.0), vec2(100.0, 100.0)};

void main(void){
	mat3x2 mat = m_LP_SD[vertex_in[0].vInstance];

	vec4 color = f_glsl_buildColor(InstanceColor[vertex_in[0].vInstance]);
	//if((vertex_in[0].vVertexIndex & 1) <= 0){
	//	color.a *= 0.5;
	//}
	uint VertexIndex = vertex_in[0].vVertexIndex;
	
	


	vec4 v = vec4(vertex_in[0].vPos, 0, 1);
	//vec4 v = vec4(0, 0, 0, 1);
	vec2 size = m_Scale * mat[2];
	ivec2 s = ivec2(0);
	vec2 stepSize = frexp(m_Scale*1.5, s);
	vec2 scale_gap = stepSize * mat[2];
	//vec2 scale_gap = size * 2;
	vec2 offset = m_Loc - ivec2(m_Loc / scale_gap) * scale_gap - scale_gap;
	//scale_gap += mat[2];

	//color = vec4(size,0,1);
	/*gl_Position = mat_ProjecView * vec4(float(VertexIndex), 0,0,1);
	gColor = color;
	EmitVertex();
	gl_Position = mat_ProjecView * vec4(600,600,0,1);
	gColor = color;
	EmitVertex();
	EndPrimitive();
	return;*/

	if((vertNum & 1) > 0) {
	//if(true) {
		vec4 vH = v;
		//点1
		vH.x *= scale_gap.x;
		vH.x += offset.x;
		
		vH.x = round(vH.x) + 0.5;
		gl_Position = mat_ProjecView * vH;
		gColor = color;
		EmitVertex();

	
		//点2
		vec4 vH2 = vH;

		vH2.y = mat[1].y;
		gl_Position = mat_ProjecView * vH2;
		//gl_Position = mat_ProjecView * vec4(600, 600, 0, 1);
		gColor = color;
		EmitVertex();

		EndPrimitive();


		
		vH.x -= scale_gap.x * 0.5;
		gl_Position = mat_ProjecView * vec4(round(vH.xy)+0.5, 0, 1);
		gColor = color * 0.3;
		EmitVertex();

		vH2.x = vH.x;
		gl_Position = mat_ProjecView * vec4(round(vH2.xy)+0.5, 0, 1);
		gColor = color * 0.3;
		EmitVertex();

		EndPrimitive();
	}
	//}

	if((vertNum & 2) > 0){
		vec4 vV = vec4(0,v.x,0,1);
		//点1
		vV.y *= scale_gap.y;
		vV.y += offset.y;
		
		vV.y = round(vV.y) + 0.5;
		gl_Position = mat_ProjecView * vV;
		gColor = color;
		EmitVertex();


		//点2
		vec4 vV2 = vV;
		vV2.x = mat[1].x;
		gl_Position = mat_ProjecView * vV2;
		gColor = color;
		EmitVertex();
		EndPrimitive();



		vV.y -= scale_gap.y * 0.5;
		gl_Position = mat_ProjecView * vec4(round(vV.xy)+0.5, 0, 1);
		gColor = color * 0.3;
		EmitVertex();

		vV2.y = vV.y;
		vV2.x = mat[1].x;
		gl_Position = mat_ProjecView * vec4(round(vV2.xy)+0.5, 0, 1);
		gColor = color * 0.3;
		EmitVertex();

		EndPrimitive();
	}

}



 